/**
 * CourseStructureViewService.java
 * com.sftz.modules.minapp.coursestructure.service
 * Copyright (c) 2018, 北京科技有限公司版权所有.
*/

package com.sftz.modules.minapp.coursestructure.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.SqlManager;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.sftz.assets.entities.InsResourceDataEntity;
import com.sftz.framework.util.CourseStructureUtil;
import com.sftz.modules.minapp.coursestructure.dto.CourseStructureDto;
import com.sftz.modules.minapp.coursestructure.dto.TreeDataDto;
import com.sftz.modules.minapp.coursestructure.form.CourseStructureForm;
import com.uxuexi.core.common.util.BeanUtil;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.db.dao.IDbDao;
import com.uxuexi.core.db.util.DbSqlUtil;

/**
 * @author   闫志彬
 * @Date	 2018年12月13日 	 
 */
@IocBean
public class CourseStructureViewService {

	@Inject
	private IDbDao dbDao;

	@Inject
	private SqlManager sqlManager;

	/**
	 * 查询课程结构列表
	 */
	public Object getCourseStructure(CourseStructureForm courseStructureForm) {
		Integer courseId = courseStructureForm.getCourseId();
		if (Util.isEmpty(courseId)) {
			throw new IllegalArgumentException("参数课程id不能为空");
		}
		List<CourseStructureDto> findCourseStructures = findCourseStructures(courseId);
		List<TreeDataDto> treeDataList = new ArrayList<TreeDataDto>();
		for (CourseStructureDto ins : findCourseStructures) {
			TreeDataDto td = new TreeDataDto();
			BeanUtil.copyProperties(ins, td);
			if (td.getPId() == 0) {
				td.setOpen(false);
			}
			treeDataList.add(td);
		}
		return treeDataList;
	}

	/**
	 * 查询课程的所有结构
	 */
	public List<CourseStructureDto> findCourseStructures(Integer courseId) {
		List<CourseStructureDto> re = new ArrayList<>();
		if (!Util.isEmpty(courseId) && courseId > 0) {
			Sql sql = Sqls.create(sqlManager.get("minapp_coursestructure_all_structure"));
			sql.params().set("courseId", courseId);
			re = DbSqlUtil.query(dbDao, CourseStructureDto.class, sql);
			//设置pid
			CourseStructureUtil.resolvePid(re);
			// 设置resourceTypeName
			CourseStructureUtil.resolveTypeName(re);
		}
		return re;
	}

	/**
	 * 查询课程结构详细信息
	 */
	public Object getCourseStructureDetails(CourseStructureForm courseStructureForm) {
		return null;
	}

	/**
	 * 直接根据层级的id 去查询具体资源信息
	 */
	public Object findCourseStructureResourceDetailsInforById(CourseStructureForm courseStructureForm) {
		Map<String, Object> obj = new HashMap<String, Object>();
		Integer resourceId = courseStructureForm.getResourceId();
		if (Util.isEmpty(resourceId)) {
			throw new IllegalArgumentException("想要查询的资源id不能为空");
		}
		InsResourceDataEntity insResourceData = dbDao.fetch(InsResourceDataEntity.class,
				Cnd.where("courseStructureId", "=", resourceId));
		if (!Util.isEmpty(insResourceData)) {
			obj.put("detailedInformation", insResourceData.getResourceUrl());
		} else {
			obj.put("detailedInformation", "");
		}
		return obj;
	}
}
